
/** 
* @Description: (用一句话描述该文件做什么) 
* @author heliang
* @date 2019-8-28 下午3:16:28 
* @version Bate1.0
*/

package org.alipay4j.component.life;

import java.util.HashMap;
import java.util.Map;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayException;
import org.alipay4j.Configuration;
import org.alipay4j.component.AbstractComponent;
import org.alipay4j.http.AlipayHttpsClient;
import org.alipay4j.http.Response;
import org.alipay4j.model.agent.Agent;
import org.alipay4j.model.agent.AgentRst;
import org.alipay4j.util.ObjectHelper;

import com.alibaba.fastjson.JSONObject;


/** 
 * @ClassName: isv代创建生活号操作组件 
 * @Description: isv代创建生活号操作组件 
 * @author heliang
 * @date 2019-8-28 下午3:16:28 
 * @version V2.1 * Update Logs: * Name: * Date: * Description: 初始化
 */
public class LifeAgentComponent extends AbstractComponent {

    public LifeAgentComponent(Alipay alipay) {
        super(alipay);
    }
    
    /**
    * @Title: isv代创建生活号接口 
    * @Description: isv代创建生活号接口
    * @author heliang
    * @param agent 生活号对象
    * @return out_biz_no 外部入驻申请单据号
    * @throws AlipayException 
     */
    @SuppressWarnings("unchecked")
    public String create(Agent agent) throws AlipayException {
        String agentStr = JSONObject.toJSONString(agent);
        Map<String, String> body = JSONObject.parseObject(agentStr, Map.class);
        body.put("method", "alipay.open.public.life.agent.create");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_life_agent_create_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_life_agent_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return bodyRes.getString("out_biz_no");
    }
    
    /**
    * @Title: isv代创建生活号申请状态查询接口 
    * @Description: isv可调用此接口查询代商户创建的生活号审核状态
    * @author heliang
    * @param out_biz_no 由开发者创建的外部入驻申请单据号
    * @return AgentRst 
    * @throws AlipayException 
     */
    public AgentRst createQuery(String out_biz_no) throws AlipayException {
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("out_biz_no", out_biz_no);
        body.put("method", "alipay.open.public.life.agentcreate.query");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_life_agentcreate_query_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_life_agentcreate_query_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        AgentRst agentRst = JSONObject.toJavaObject(bodyRes, AgentRst.class);
        return agentRst;
    }
    

}
